package com.itheima.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.constant.TokenConstant;
import com.itheima.domain.Employee;
import com.itheima.domain.vo.EmployeeVo;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
 * @author licheng
 * @date 2022/8/7
 * 实现接口并继承带有默认sql方法的类
 */

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {


    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    public R<Employee> adminEmployeeLogin(Employee employee) {
        // 去数据库查询满足条件的员工
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername())
                .eq(Employee::getPassword, DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()));
        Employee emp = getOne(lqw);
        if (emp == null) {
            return R.error("用户不存在或密码错误");
        }
        //获取唯一令牌
        String tokenString = "" + System.currentTimeMillis() + emp.getId();
        //加密
        String token = DigestUtils.md5DigestAsHex(tokenString.getBytes(StandardCharsets.UTF_8));
        //往redis数据库中添加键值对并设置存在时间
        redisTemplate.opsForValue().set(TokenConstant.EMPLOYEE_TOKEN_PREFIX + token, JSON.toJSONString(emp), 300, TimeUnit.MINUTES);

        EmployeeVo employeeVo = new EmployeeVo();
        //从emp复制内容到employeeVo中
        BeanUtils.copyProperties(emp, employeeVo);
        employeeVo.setToken(token);

        return R.success(employeeVo);
    }

    @Override
    public R<String> adminEmployeeLoginOut(String token) {
        //删除数据库中的值通过key
        redisTemplate.delete(TokenConstant.EMPLOYEE_TOKEN_PREFIX + token);
        return R.success("退出成功");
    }


    @Override
    public R<String> adminEmployeeSave(Employee employee) {
        // 调用存储方法
        boolean resultSave = save(employee);
        if (resultSave){
        return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    @Override
    public R<Employee> adminEmployeeSelectById(String id) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getId, id);
        Employee employee = getOne(lqw);
        return R.success(employee);
    }
    @Override
    public R<Page<Employee>> adminEmployeePageSelect(String name,Integer page,  Integer pageSize) {
        // 构造分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        // 构造条件
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name)
                .or()
                .like(StringUtils.isNotEmpty(name), Employee::getUsername, name);
        // 添加排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        // 执行查询
        page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }
    @Override
    public R<String> adminUpdateEmployee (String token,  Employee employee) {
        boolean updateResult = updateById(employee);
        if (updateResult) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }


}
